package st.business.tepth.business.user.busnumber.workholder.repair.work;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.SupportFragment;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnMultiItemChooseListener;
import st.business.tepth.business.bean.ClassificationBean;
import st.business.tepth.business.bean.ClassificationTypeBean;
import st.business.tepth.business.businfo.BusInfoDelegate;
import st.business.tepth.business.event.StartBrotherEvent;
import st.business.tepth.business.user.busnumber.workholder.adapter.ClassificationAdapter;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogAllTrouble;
import st.business.tepth.business.user.busnumber.workholder.dialog.DialogCheckResult;
import st.business.tepth.business.user.busnumber.workholder.repair.work.content.RepairDelegate;
import st.shadow.library.core.base.fragment.BaseFragment;
import st.shadow.library.ui.recycler.MultipleItemEntity;


/**
 * @author zhangyu
 * @date 2019/1/14
 * 小修作业界面（分系+content）
 */
public class WorkDelegate extends BaseFragment {
    @BindView(R2.id.recycler_view_tab)
    RecyclerView mRecyclerViewTab;
    @BindView(R2.id.tv_work_type)
    TextView mTvWorkType;
    private ClassificationTypeBean mTypeBean;
    private int mType;
    private int mRepairId;
    private ClassificationAdapter mClassificationAdapter;

    public static WorkDelegate newInstance(ClassificationTypeBean typeBean,
                                           int repairId,
                                           int type) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("typeBean", typeBean);
        bundle.putInt("type", type);
        bundle.putInt("repairId", repairId);
        WorkDelegate delegate = new WorkDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mTypeBean = bundle.getParcelable("typeBean");
            mType = bundle.getInt("type");
            mRepairId = bundle.getInt("repairId");
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_user_work;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initView();
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        initData();
    }

    private void initView() {
        mRecyclerViewTab.setLayoutManager(new LinearLayoutManager(_mActivity));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerViewTab.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        String type = "";
        switch (mType) {
            case 1:
                type = "小修";
                break;
            case 2:
                type = "一级保养";
                break;
            case 3:
                type = "二级保养";
                break;
            case 4:
                type = "三级保养";
                break;
            case 5:
                type = "电池保养";
                break;
        }
        mTvWorkType.setText(String.format("%s作业中", type));
    }

    private void initData() {
        if (mTypeBean != null) {
            final ArrayList<ClassificationBean> classificationBeans =
                    mTypeBean.getVehicleSeriesList();
            if (classificationBeans != null && !classificationBeans.isEmpty()) {
                List<MultipleItemEntity> multipleItemEntities = new ArrayList<>();
                for (int i = 0, size = classificationBeans.size(); i < size; i++) {
                    ClassificationBean repairTaskBean = classificationBeans.get(i);
                    multipleItemEntities.add(MultipleItemEntity.builder()
                            .setField("name", repairTaskBean.getName())
                            .setField("id", repairTaskBean.getId())
                            .setField("finish", 0)
                            .setField("unSolveCount", repairTaskBean.getUnSolveCount())
                            .setField("checked", i == 0)
                            .setField("count", repairTaskBean.getCount())
                            .build());
                }
                mRecyclerViewTab.setAdapter(mClassificationAdapter =
                        new ClassificationAdapter(R.layout.item_user_classification,
                                multipleItemEntities, new OnMultiItemChooseListener<Integer, Integer>() {
                            @Override
                            public void onChoose(Integer position, Integer id) {
                                //小修
                                SupportFragment contentFragment = findChildFragment(RepairDelegate.class);
                                if (contentFragment != null) {
                                    contentFragment.replaceFragment(RepairDelegate
                                            .newInstance(mRepairId, id,
                                                    mTypeBean.getOperationState(),
                                                    position), true);
                                }
                            }
                        }, true));
                ClassificationBean first = classificationBeans.get(0);
                //小修
                if (findChildFragment(RepairDelegate.class) == null) {
                    loadRootFragment(R.id.view_content,
                            RepairDelegate
                                    .newInstance(mRepairId, first.getId(),
                                            mTypeBean.getOperationState(), 0),
                            false, false);
                }

            }
        }
    }

    public void changeTroubleCount(int count, int position) {
        if (mClassificationAdapter != null) {
            MultipleItemEntity entity = mClassificationAdapter.getItem(position);
            if (entity != null) {
                entity.setField("unSolveCount", count);
                mClassificationAdapter.notifyItemChanged(position);
            }
        }
    }

    @OnClick(R2.id.tv_bus_info)
    public void onShowBusInfo() {
        EventBus.getDefault().post(new StartBrotherEvent(BusInfoDelegate.newInstance(mRepairId)));
    }

    @OnClick(R2.id.tv_all_trouble)
    public void onShowTrouble() {
        new DialogAllTrouble(_mActivity, mRepairId).show();
    }

    @OnClick(R2.id.tv_check_result)
    public void onCheckResult() {
        new DialogCheckResult(_mActivity, mRepairId).measureSize().show();
    }

    public void refreshData() {

    }

}
